﻿using Microsoft.AspNetCore.Mvc;
using PlatformNetCore.Common;
using PlatformNetCore.Common.Helper;
using PlatformNetCore.Entity;
using PlatformNetCore.IService;
using PlatformNetCore.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace PlatformNetCore.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class VideosController : BaseController
    {
        private readonly IVideosService videosService;
        public VideosController(IVideosService _videosService)
        {
            videosService = _videosService;
        }

        #region 视频管理
        [RoleAuthorize("VideosList")]
        public IActionResult VideosList(string search = "",string videoType = "", int pageindex = 1, int pagesize = 15)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<Videos, bool>> predicate = s => s.IsDel == 0;
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.Title.Contains(search));
                    }
                    if (!string.IsNullOrWhiteSpace(videoType))
                    {
                        predicate = predicate.And(k => k.VideoType == videoType);
                    }
                    return Json(videosService.GetVideosPagedList(pageindex, pagesize, predicate));
                }
                ViewBag.VideoTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(VideoType), true);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("VideosAdd")]
        public IActionResult VideosAdd()
        {
            try
            {
                Videos model = new Videos();
                ViewBag.VideoTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(VideoType), true);
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("VideosAdd")]
        public IActionResult VideosAdd(Videos model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(videosService.AddVideos(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("VideosDel")]
        public IActionResult VideosDel(string id)
        {
            try
            {
                List<string> ids = new List<string>() { id };
                return Json(videosService.UpdateVideosState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 拷贝
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("VideosCopy")]
        public IActionResult VideosCopy(string id)
        {
            try
            {
                return Json(videosService.CopyVideos(id, CurrentUser.Login));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("VideosEdit")]
        public IActionResult VideosEdit(string id)
        {
            try
            {
                ResultResponse<Videos> result = videosService.GetVideos(id);
                ViewBag.VideoTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(VideoType), true);
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("VideosEdit")]
        public IActionResult VideosEdit(Videos model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(videosService.UpdateVideos(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

    }
}